package entity

import (
	"fmt"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
	"shqsoft.cn/pas3ui/workflow"
	"shqsoft.cn/pas3ui/workflow/entity"
)

//IndiAccoWorkflowNewAppHis 零售新客户认定审批
type IndiAccoWorkflowNewAppHis struct {
	ID       int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	IDNO     string    `gorm:"column:IDNO"`
	CUNM     string    `gorm:"column:CUNM"`
	CUID     int       `gorm:"column:CUID"`
	ACID     int       `gorm:"column:ACID"`
	ACNO     string    `gorm:"column:ACNO"`
	BANO     string    `gorm:"column:BANO"`
	ITNM     string    `gorm:"column:ITNM"`
	EMNO1    string    `gorm:"column:EMNO1"`
	EMID1    int       `gorm:"column:EMID1"`
	EMNM1    string    `gorm:"-" json:"emnm1"`
	DSVL1    float64   `gorm:"column:DSVL1"`
	EMNO2    string    `gorm:"column:EMNO2"`
	EMNM2    string    `gorm:"-" json:"emnm2"`
	EMID2    int       `gorm:"column:EMID2"`
	DSVL2    float64   `gorm:"column:DSVL2"`
	EMNO3    string    `gorm:"column:EMNO3"`
	EMNM3    string    `gorm:"-" json:"emnm3"`
	EMID3    int       `gorm:"column:EMID3"`
	DSVL3    float64   `gorm:"column:DSVL3"`
	EMNO4    string    `gorm:"column:EMNO4"`
	EMNM4    string    `gorm:"-" json:"emnm4"`
	EMID4    int       `gorm:"column:EMID4"`
	DSVL4    float64   `gorm:"column:DSVL4"`
	UID      int       `gorm:"column:UID"`
	FRID     int       `gorm:"column:FRID"`
	WFID     int       `gorm:"column:WFID"`
	OPDT     time.Time `gorm:"column:OPDT"`
	UTNM     string    `gorm:"column:UTNM"`
	SQR      string    `gorm:"column:SQR"`
	SQDT     time.Time `gorm:"column:SQDT"`
	STEP     int       `gorm:"-" json:"step"`
	Progress float64   `gorm:"-" json:"progress"`
	StepName string    `gorm:"-" json:"stepName"`
	Status   string    `gorm:"-" json:"status"`
}

//TableName 对公客户认定审批
func (IndiAccoWorkflowNewAppHis) TableName() string {
	return "T_INDI_CLIE_DIVI_HIS"
}

// AfterFind 查询后触发器
func (i *IndiAccoWorkflowNewAppHis) AfterFind(tx *gorm.DB) (err error) {
	ec := base.GetEmployeeCache()
	i.EMNM1, err = ec.GetNameByID(i.EMID1)
	if err != nil {
		return err
	}
	if i.DSVL2 > 0 {
		i.EMNM2, err = ec.GetNameByID(i.EMID2)
		if err != nil {
			return err
		}
	}
	if i.DSVL3 > 0 {
		i.EMNM3, err = ec.GetNameByID(i.EMID3)
		if err != nil {
			return err
		}
	}
	if i.DSVL4 > 0 {
		i.EMNM4, err = ec.GetNameByID(i.EMID4)
		if err != nil {
			return err
		}
	}
	i.IDNO = base.IndoTM(i.IDNO)
	i.ACNO = base.AcnoTM(i.ACNO)
	i.BANO = base.AcnoTM(i.BANO)
	if i.FRID == 0 {
		i.Status = "exception"
		i.StepName = "校验错误"
		return
	}
	run := entity.FlowRunHis{}
	if err = tx.Find(&run, i.FRID).Error; err != nil {
		tx.Rollback()
		return
	}
	wf := workflow.GetWorkFlow(run.WFID)
	if wf == nil {
		return fmt.Errorf("指定的流程ID:%d在流程配置中不存在！", run.WFID)
	}
	act := wf.GetAction(run.Step)
	if act == nil {
		return fmt.Errorf("指定的stepID:%d在流程%s配置中不存在！", run.Step, wf.Name)
	}
	count := len(wf.Actions)
	i.STEP = run.Step
	i.StepName = "审批中"
	if i.STEP == 1 {
		i.StepName = "申请中"
	}
	i.Progress = float64(act.Step*100) / float64(count)
	if i.Progress >= 100 && run.State == "0" {
		i.Progress = 95
	}
	i.Status = ""
	if run.State == "0" {
		i.Status = "active"
	} else if run.State == "2" {
		i.StepName = "已中止"
		i.Status = "exception"
	} else {
		i.StepName = "已完成"
		i.Status = "success"
	}
	return
}

func IndiAccoWorkflowNewAppHisEF() base.EntityFunc {
	return func() interface{} {
		entity := IndiAccoWorkflowNewAppHis{}
		return &entity
	}
}
func IndiAccoWorkflowNewAppHisESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []IndiAccoWorkflowNewAppHis
		return &entities
	}
}

func init() {
	// dao := base.NewDefaultDao(IndiAccoWorkflowNewAppHisEF(), IndiAccoWorkflowNewAppHisESF(), nil, "my")
	// filter := base.WorkFolowFilter{
	// 	FilterColName: "FRID",
	// }
	// ei, err := workflow.GetEntityInfo("IndiAccoWorkflowNewApp")
	// if err != nil {
	// 	panic(err)
	// }
	// filter.SetAdditional("wfid", ei.WfidsToString())
	// filter.SetAdditional("manager", ei.MgMapToString())
	// dao.Fileter = &filter
	// dao.Fileter.SetDB("my")
	// base.DaoPoolObj.Put("IndiAccoWorkflowNewAppHis", dao)
}
